package com.lvhaosb.newpneumonia.service.impl;

import com.lvhaosb.newpneumonia.POJO.vo.DomesticEpidemicInfo;
import com.lvhaosb.newpneumonia.POJO.po.DomesticEpidemic;
import com.lvhaosb.newpneumonia.mapper.DomesticEpidemicMapper;
import com.lvhaosb.newpneumonia.service.DomesticEpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 国内疫情 服务实现类
 *
 * @author lvhao
 * @since 2020-06-21
 */
@Service
public class DomesticEpidemicServiceImpl implements DomesticEpidemicService {
    @Autowired
    private DomesticEpidemicMapper domesticEpidemicMapper;

    @Override
    public DomesticEpidemicInfo queryDomesticEpidemicInfo() {
        if(!domesticEpidemicMapper.selectByTimeNewest().isEmpty()){
            //查询最新的国内疫情信息，返回列表取其中一个防止因数据过多发生错误
            List<DomesticEpidemic> domesticEpidemicsList = domesticEpidemicMapper.selectByTimeNewest();
            DomesticEpidemic domesticEpidemic = domesticEpidemicsList.get(0);
            //赋值和返回
            DomesticEpidemicInfo domesticEpidemicInfo = new DomesticEpidemicInfo();
            domesticEpidemicInfo.setTime(domesticEpidemic.getTime().toString());//时间
            domesticEpidemicInfo.setAsymptomatic(domesticEpidemic.getAsymptomatic());//无症状
            domesticEpidemicInfo.setCumulativeCure(domesticEpidemic.getCumulativeCure());//累计治愈
            domesticEpidemicInfo.setCumulativeDeath(domesticEpidemic.getCumulativeDeath());//累计死亡
            domesticEpidemicInfo.setCumulativeDiagnosis(domesticEpidemic.getCumulativeDiagnosis());//累计确诊
            domesticEpidemicInfo.setCurrentDiagnosis(domesticEpidemic.getCurrentDiagnosis());//现有确诊
            domesticEpidemicInfo.setExistingSevere(domesticEpidemic.getExistingSevere());//现有重症
            domesticEpidemicInfo.setExistingSuspects(domesticEpidemic.getExistingSuspects());//现有疑似
            domesticEpidemicInfo.setOverseasImport(domesticEpidemic.getOverseasImport());//境外输入
            return domesticEpidemicInfo;
        }
        //数据库无数据
        return null;
    }

    @Override
    @Transactional
    public Boolean save(DomesticEpidemicInfo domesticEpidemicInfo) {
        //赋值
        DomesticEpidemic domesticEpidemic = new DomesticEpidemic();
        domesticEpidemic.setTime(LocalDateTime.now());//时间
        domesticEpidemic.setAsymptomatic(domesticEpidemicInfo.getAsymptomatic());//无症状
        domesticEpidemic.setCumulativeCure(domesticEpidemicInfo.getCumulativeCure());//累计治愈
        domesticEpidemic.setCumulativeDeath(domesticEpidemicInfo.getCumulativeDeath());//累计死亡
        domesticEpidemic.setCumulativeDiagnosis(domesticEpidemicInfo.getCumulativeDiagnosis());//累计确诊
        domesticEpidemic.setCurrentDiagnosis(domesticEpidemicInfo.getCurrentDiagnosis());//现有确诊
        domesticEpidemic.setExistingSevere(domesticEpidemicInfo.getExistingSevere());//现有重症
        domesticEpidemic.setExistingSuspects(domesticEpidemicInfo.getExistingSuspects());//现有疑似
        domesticEpidemic.setOverseasImport(domesticEpidemicInfo.getOverseasImport());//境外输入
        if(domesticEpidemicMapper.insert(domesticEpidemic)>0){
            return true;
        }
        return false;
    }
}
